package org.sgx.gapitest.client.test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.sgx.gapi.client.GAPI;
import org.sgx.gapi.client.apis.drive.DriveModule;
import org.sgx.gapi.client.apis.tasks.tasklists.ListRequest;
import org.sgx.gapi.client.apis.tasks.tasklists.ListResponse;
import org.sgx.gapi.client.apis.tasks.tasklists.TaskList;
import org.sgx.gapi.client.apis.tasks.tasks.Task;
import org.sgx.gapi.client.auth.AuthResponse;
import org.sgx.gapi.client.client.GAPICallback;
import org.sgx.gapi.client.loader.AuthDefinition;
import org.sgx.gapi.client.loader.AuthUITrigger;
import org.sgx.gapi.client.loader.GAPILoader;
import org.sgx.gapi.client.loader.GAPILoaderCallback;
import org.sgx.gapi.client.loader.ModuleDefinition;
import org.sgx.gapi.client.util.GAPILoadCallback;
import org.sgx.gapi.client.util.GAPIUtil;
import org.sgx.gapitest.client.AbstractTest;
import org.sgx.gapitest.client.GAPITestConstants;
import org.sgx.gapitest.client.GAPITestTextResource;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * in this example we test drive.about API
 * 
 * @author sg
 * 
 */
public class TaskList1 extends AbstractTest {

	private String apiKey;
	private String scope;
	private String clientId;
	private Button authButton;

	@Override
	public void test(Element parent) {
		GAPI.load(new GAPILoadCallback() {
			@Override
			public void loaded() {
				main();
			}
		});
	}

	protected void main() {

		authButton = new Button("authenticate");
		RootPanel.get().add(authButton);
		AuthUITrigger authUITrigger = GAPIUtil.buildAuthUITriggerFrom(authButton);

		clientId = GAPITestConstants.CLIENT_ID;

		scope = "https://www.googleapis.com/auth/tasks";

		apiKey = GAPITestConstants.API_KEY;

		GAPI.get().client().setApiKey(apiKey);

		ModuleDefinition moduleDef = new DriveModule();
		AuthDefinition authDefinition = new AuthDefinition(clientId, scope, authUITrigger);
		GAPILoader loader = new GAPILoader(authDefinition, moduleDef);
		loader.load(new GAPILoaderCallback() {

			@Override
			public void loaded(AuthResponse authResp) {
				callApi();
			}
		});

	}

	protected void callApi() {
		new ListRequest().execute(new GAPICallback<ListResponse>() {

			@Override
			public void call(ListResponse result) {
				if (result.error() != null) {
					Window.alert("Error on TaskList list request: " + result.error().message());
				} else {
					Collection<TaskList> items = result.itemsCol();
					String titles = "";
					for (TaskList s : items)
						titles += s.title() + ", ";
					Window.alert("TaskList list response first " + items.size() + " titles : " + titles);
					if (items.size() > 0) {
						doListAllTasks(items.iterator().next());
					}
				}
			}
		});
	}

	protected void doListAllTasks(final TaskList l) {
		org.sgx.gapi.client.apis.tasks.tasks.ListRequest req = new org.sgx.gapi.client.apis.tasks.tasks.ListRequest();
		req.setTasklist(l.id());
		req.execute(new GAPICallback<org.sgx.gapi.client.apis.tasks.tasks.ListResponse>() {
			@Override
			public void call(org.sgx.gapi.client.apis.tasks.tasks.ListResponse result) {
				if (result.error() != null) {
					Window.alert("Error listing tasks on takslist named " + l.id() + ", reason: "
							+ result.error().message());
				} else {
					Collection<Task> items = result.itemsCol();
					String titles = "";
					for (Task s : items)
						titles += s.title() + ", ";
					Window.alert("Tasks " + items.size() + " titles : " + titles);
				}
			}
		});
	}

	@Override
	public String getId() {
		return "TaskList1";
	}

	@Override
	public String getDescription() {
		return "show how to call Task's TaskList list";
	}

	@Override
	public Map<String, GAPITestTextResource> getResources() {
		HashMap<String, GAPITestTextResource> m = new HashMap<String, GAPITestTextResource>();
		m.put("DriveTest1.java", new GAPITestTextResource(TestResources.INSTANCE.DriveTest1()));
		return m;
	}

	@Override
	public String[] getTags() {
		return null;
	}

}
